#include <iostream>
#include <vector>
#include<unordered_map>
#include <algorithm>
using namespace std;
/**
 * @brief
 * 1 <= nums.length <= 2 * 104
-109 <= nums[i] <= 109
 *
 */
class Solution
{
public:
    int findLHS(vector<int> &nums)
    {
         sort(nums.begin(),nums.end());
        int begin = 0;
        int res = 0;
        for (int end = 0; end < nums.size(); end++) {
            while (nums[end] - nums[begin] > 1) {
                begin++;
            }
            if (nums[end] - nums[begin] == 1) {
                res = max(res, end - begin + 1);
            }
        }
        return res;
    }

     int findLHS_2(vector<int>& nums) {
        unordered_map<int, int> cnt;
        int res = 0;
        for (int num : nums) {
            cnt[num]++;
        }
        for (auto [key, val] : cnt) {
            if (cnt.count(key + 1)) {
                res = max(res, val + cnt[key + 1]);
            }
        }
        return res;
    }
};
int main()
{
    Solution s;
    vector<int> nums = {1, 3, 2, 2, 5, 2, 3, 7};
    cout << s.findLHS_2(nums) << endl;
    return 0;
}